package com.example.demo.exceptionmsg;

import com.example.demo.response.JsonResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalDefultExceptionHandler {
    //声明要捕获的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public <T> JsonResponse<?> defultExcepitonHandler(Exception e) {
        if (e instanceof BusinessException) {
            //log.error("business  exception is:>>>>>>：" + e.toString());
            BusinessException businessException = (BusinessException) e;
            return JsonResponse.error(businessException.getMessage());
        }else if(e instanceof BeaconExcepiton){
            BeaconExcepiton beaconExcepiton=(BeaconExcepiton)e;
            return JsonResponse.error(beaconExcepiton.getMessage());
        }
        //未知错误
        //log.error("exception is:>>>>>>：" + e.toString());
        return JsonResponse.error(500);
    }
//
//    //声明要捕获的异常
//    @ExceptionHandler(BeaconExcepiton.class)
//    @ResponseBody
//    public <T> JsonResponse<?> defultExcepitonHandler(BeaconExcepiton e) {
//        int a=0;
//        return JsonResponse.error(403);
//    }
}
